/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.KhachHang_DTO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Phung
 */
public class KhachHang_DAO {
    
    public static String Get_MaKH() throws SQLException{
        String MaKH = null;
        MySQLDataHelper_DAO.open();
        Connection Con = MySQLDataHelper_DAO.getConnection();
        CallableStatement statement = Con.prepareCall("{call Add_CT_ID(?)}");
        statement.setString(1, "DT");
        statement.execute();
        ResultSet Rs = statement.getResultSet();
        if(Rs.next()){
            MaKH = Rs.getString(1);
        }
        return MaKH;
    }
    
    public static ArrayList<KhachHang_DTO> GetALL()
    {
        ArrayList<KhachHang_DTO> ds = new ArrayList<>();
        try {
            String sql = "Select * from KhachHang" ;
            MySQLDataHelper_DAO.open();
            ResultSet rs =  MySQLDataHelper_DAO.executeQuery(sql);
            while (rs.next()) {
                KhachHang_DTO KhachHang = new KhachHang_DTO();
                KhachHang.setCMND(rs.getString("CMND"));
                KhachHang.setHoTen(rs.getString("HoTen"));
                KhachHang.setDiaChi(rs.getString("DiaChi"));
                KhachHang.setNgaySinh(rs.getDate("NgaySinh"));
                ds.add(KhachHang);
            }
            MySQLDataHelper_DAO.close();
       }catch (SQLException ex) {
            System.out.println(ex);
        }
        return ds;
    }
    
    public static boolean Insert(KhachHang_DTO KhachHang)
    {
        boolean flag = false;
        try {
            java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd");
            String NgaySinh = fmt.format(KhachHang.getNgaySinh());
            String sql = "Insert KhachHang Values('"+KhachHang.getCMND()+"','"+KhachHang.getHoTen()+"','"+KhachHang.getDiaChi()+"','"+NgaySinh+"')";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Update(KhachHang_DTO KhachHang)
    {
        boolean flag = false;
        try {
            java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd");
            String NgaySinh = fmt.format(KhachHang.getNgaySinh());
            String sql = "Update KhachHang Set HoTen = '"+ KhachHang.getHoTen() +"', DiaChi = '"+KhachHang.getDiaChi()+"', NgaySinh = '"+ NgaySinh +"' where CMND = '"+ KhachHang.getCMND()+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Delete(String maKH)
    {
        boolean flag = false;
        try {
            String sql = "Delete from KhachHang where CMND = '"+maKH+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
}
